package com.hopu.controller;

import com.hopu.entity.Result;
import com.hopu.entity.User;
import com.hopu.service.UserService;
import com.hopu.utils.JwtUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;

@RestController
@Slf4j
public class LoginController {

    @Autowired
    private JwtUtils jwtUtils;

    @Autowired
    private UserService userService;

    /**
     * 登录
     *
     * @param user
     * @return
     */
    @PostMapping("/login")
    public Result login(@RequestBody User user) {
        User u = userService.login(user);
        if (u != null) {
            HashMap<String, Object> claims = new HashMap<>();
            claims.put("userid",u.getUserId());
            claims.put("password",u.getPassword());
            String jwt = jwtUtils.generateJwt(claims);
            return Result.success(jwt);
        }
        return Result.error("用户名或密码错误");
    }
}
